<?php
/**
 * Cookie操作类
 * @copyright (c)2011-2012 php300.com
 * @license   dxtoo.cn
 * @author    lnbalife@126.com
 * @version   1.0beta
 */
class Cookie
{
    static $cookie_prefix='m_';
    static $cookie_domain='';
    
    /**
     * 判断Cookie是否存在
     * @param string $name Cookie名称
     */
    static function is_set($name) {
        return isset($_COOKIE[$cookie_prefix.$name]);
    }

    
	/**
	 * 获取Cookie值
	 * @param string $name Cookie名称
	 */
    static function get($name) {
        $value   = $_COOKIE[$cookie_prefix.$name];
        $value   =  unserialize(base64_decode($value));
        return $value;
    }

    /**
     * 设置Cookie值
     * @param string $name
     * @param unknown_type $value
     * @param unknown_type $expire
     * @param unknown_type $path
     * @param unknown_type $domain
     */
    static function set($name,$value,$expire='',$path='',$domain='') {
        if($expire=='') {
            $expire =0;
        }
        if(empty($path)) {
            $path = '/';
        }
        if(empty($domain)) {
            $domain =   $cookie_domain;
        }
        $expire =!empty($expire)?time()+$expire:0;
        if(!empty($value))
            $value=base64_encode(serialize($value));
        setcookie($cookie_prefix.$name, $value,$expire,$path,$domain);
        $_COOKIE[$cookie_prefix.$name]  =   $value;
    }

    /**
     * 删除Cookie
     * @param unknown_type $name
     * @param unknown_type $cookiepath
     */
    static function delete($name,$cookiepath='') {
        if($cookiepath!=""){
            Cookie::set($name,'',time()-3600,"",$cookiepath);
        }else{
            Cookie::set($name,'',time()-3600);
        }
        unset($_COOKIE[$cookie_prefix.$name]);
    }

    /**
     * 清空所有Cookie
     */
    static function clear() {
        unset($_COOKIE);
    }
}

?>
